
<template>
  <div class="center" :style="{ 'background-image': `url(${bgPath})` }">
    <!-- <h1>笔记本</h1> -->
    <div class="logon">
      <div :class="overlaylong" v-loading="loginLoading">
        <div v-show="disfiex == 0">
          <div v-show="!isQrCode" class="overlaylong-Signin">
            <h2 class="overlaylongH2">登录笔记本</h2>
            <input
              type="text"
              v-model.trim="loginForm.userName"
              placeholder="手机号码"
            />
            <input
              type="password"
              v-model.trim="loginForm.passWord"
              placeholder="密码"
            />

            <button
              class="inupbutton"
              :loading="loginLoading"
              @click="handleLogin"
            >
              登 录
            </button>
          </div>
          <div v-show="isQrCode" class="overlaylong-Signin">
            <h2 class="overlaylongH2">登录笔记本</h2>
            <h4 class="overlaylongH4">钉钉登录</h4>
            <div
              id="self_defined_element"
              style="margin-top: 0px"
              class="self-defined-classname"
            ></div>
          </div>
          <div class="toggleLogin" @click="isQrCode = !isQrCode">
            <img
              :src="`/login${!isQrCode ? 1 : 2}.png`"
              width="50px"
              height="50px"
            />
          </div>
        </div>
        <div class="overlaylong-Signup" v-if="disfiex == 1">
          <h2 class="overlaylongH2">注册笔记本</h2>
          <input
            type="text"
            v-model.trim="loginForm.userName"
            placeholder="手机号"
          />
          <input
            type="text"
            v-model.trim="loginForm.passWord"
            placeholder="短信验证码"
          />
          <button class="inupbutton" @click="handleLogin">注 册</button>
        </div>
      </div>
      <div :class="overlaytitle">
        <div class="overlaytitle-Signin" v-if="disfiex == 0">
          <h2 class="overlaytitleH2">嗨，朋友！</h2>
          <p class="overlaytitleP">输入您的个人信息，开启精彩旅程吧~</p>
          <!-- <div class="buttongohs" @click="Signin">注 册</div> -->
        </div>
        <div class="overlaytitle-Signup" v-if="disfiex == 1">
          <h2 class="overlaytitleH2">Welcome Back!</h2>
          <p class="overlaytitleP">
            To keep connected with us please login with your personal info
          </p>
          <div class="buttongohs" @click="Signup">Sign in</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { ElMessage } from "element-plus";
import { dingTalklogin, login } from "@/api/user";
import { useSocket } from "@/utils/socket/useSocket";
import MySender from "@/utils/socket/MySender";
import MsgCommand from "@/utils/socket/enums/MsgCommand";

export default {
  data() {
    return {
      overlaylong: "overlaylong",
      overlaytitle: "overlaytitle",
      disfiex: 0,
      bgPath: "https://bing.img.run/1920x1080.php",
      loginLoading: false,
      loginForm: {
        userName: "",
        passWord: "",
      },
      redirectUri: location.href, // 扫码后回调URL
      appId: "dingib34wme44bawkonu", // 应用的AppKey
      state: "test", // 随便指定，扫码后会原样返回
      isQrCode: true,
    };
  },
  mounted() {
    var that = this;
    this.$ddLogin(
      {
        id: "self_defined_element",
        width: 300,
        height: 300,
      },
      {
        redirect_uri: encodeURIComponent(this.redirectUri),
        client_id: this.appId,
        scope: "openid", // 固定值
        response_type: "code", // 固定值
        state: this.state,
        prompt: "consent", // 值为consent时需要授权确认
      },
      async (loginResult) => {
        console.log(loginResult);
        const { redirectUrl, authCode, state } = loginResult;
        if (that.loginLoading) {
          return false;
        }
        that.loginLoading = true;
        const res = await dingTalklogin({ code: authCode });
        that.loginLoading = false;
        if (res.status === 0) {
          this.loginSuccess(res.data);
        } else {
          ElMessage.error(res.message);
        }
      },
      (errorMsg) => {
        //debugger;
        // 这里一般需要展示登录失败的具体原因
        alert(`Login Error: ${errorMsg}`);
      }
    );
  },
  methods: {
    Signin() {
      this.overlaylong = "overlaylongleft";
      this.overlaytitle = "overlaytitleright";
      setTimeout(() => {
        this.disfiex = 1;
      }, 200);
    },
    Signup() {
      this.overlaylong = "overlaylongright";
      this.overlaytitle = "overlaytitleleft";

      setTimeout(() => {
        this.disfiex = 0;
      }, 200);
    },
    async handleLogin() {
      this.loginLoading = true;
      if (this.loginForm.userName === "") {
        ElMessage.error("用户名不能为空！");
        return false;
      }
      if (this.loginForm.passWord === "") {
        ElMessage.error("密码不能为空！");
        return false;
      }
      let res = await login(this.loginForm);
      this.loginLoading = false;
      if (res.status == 0) {
        this.loginSuccess(res.data);
      } else {
        ElMessage.error(res.message);
      }
    },
    loginSuccess(data) {
      const toLogin = (data) => {
        setTimeout(() => {
          this.$router.push("/home");
        }, 1000);
      };
      sessionStorage.setItem("token", data.tokenStr);
      ElMessage.success("登录成功，即将跳转~");
      setTimeout(() => {
        toLogin(data);
      }, 100);
    },
  },
};
</script>
<style scoped>
.self-defined-classname {
  width: 300px;
  height: 300px;
}

.center {
  width: 1920px;
  height: 100vh;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  color: #666666;
}

h1 {
  font-size: 30px;
  color: black;
}

.logon {
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  /* position: relative;
		overflow: hidden; */
  width: 768px;
  max-width: 100%;
  min-height: 480px;
  margin-top: 20px;
  display: flex;
  background: -webkit-linear-gradient(right, #824c96, #4284db);
}

.overlaylong {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.toggleLogin {
  position: absolute;
  right: 0;
  top: 0;
  height: 50px;
  width: 50px;
}

.toggleLogin img {
  cursor: pointer !important;
}

.overlaylongleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  transform: translateX(100%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaylongright {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaytitle {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaytitleH2 {
  font-size: 30px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitleP {
  font-size: 15px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitleleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
}

.overlaytitleright {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(-100%);
  transition: transform 0.6s ease-in-out;
}

.overlaytitle-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.overlaytitle-Signup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.buttongohs {
  width: 180px;
  height: 40px;
  border-radius: 50px;
  border: 1px solid #fff;
  color: #fff;
  font-size: 15px;
  text-align: center;
  line-height: 40px;
  margin-top: 40px;
}

.overlaylongH2 {
  font-size: 25px;
  color: #666666;
  /* width: 250px; */
}

.overlaylongH4 {
  font-size: 16px;
  color: #666666;
  margin-top: 10px;
  /* width: 250px; */
}

.overlaylong-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.overlaylong-Signup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

input {
  background-color: #eee;
  border: none;
  padding: 12px 15px;
  margin: 10px 0;
  width: 240px;
}
h3 {
  font-size: 10px;
  margin-top: 10px;
  cursor: pointer;
}
.inupbutton {
  background-color: #824c96;
  border: none;
  width: 180px;
  height: 40px;
  border-radius: 50px;
  font-size: 15px;
  color: #fff;
  text-align: center;
  line-height: 40px;
  margin-top: 30px;
  cursor: pointer;
}
</style>
 